package board.grid;

import java.util.ArrayList;
/** A Grid<T> used to with a given range for rows and columns.  
 *  Only spaces within that range are valid.  
 * 
 * @see Grid
 * 
 * @author R.Wisniewski1101
 *
 * @param <T> - the type of occupants in this Grid
 */
public class RangeGrid<T> extends AbstractGrid<T> {
	/** The first column number on the Grid.  */
	private int start_column;
	/** The last column number on the Grid.  */
	private int end_column;
	/** The first row number on the Grid.  */
	private int start_row;
	/** The last row number on the Grid.  */
	private int end_row;
	/** Creates a Grid with a the give end
	 *  column and row.  This Grid will start
	 *  at the origin.  
	 * 
	 * @throws IllegalArgumentException - the column or row are negative and therefore less than the start column or row respectively.  
	 * @param column - the last column number.  
	 * @param row - the last row number
	 */
	public RangeGrid(int column, int row) {
		if(column < 0)
			throw new IllegalArgumentException("There must be at least one column.  ");
		if(row < 0)
			throw new IllegalArgumentException("There must be at least one row.  ");
		start_column = 0;
		end_column = column;
		start_row = 0;
		end_row = row;
	}
	/** Construct a RangeGrid with the give start and end row and column numbers.  
	 * 
	 * @throws IllegalArgumentException - the start column or row is greater than the end column or row.  
	 * @param start_column
	 * @param end_column
	 * @param start_row
	 * @param end_row
	 */
	public RangeGrid(int start_column, int end_column, int start_row, int end_row) {
		if(end_column < start_column)
			throw new IllegalArgumentException("The end column must be after the start column.  ");
		if(end_row < start_row)
			throw new IllegalArgumentException("The end row must be after the start row.  ");
		this.start_column = start_column;
		this.end_column = end_column;
		this.start_row = start_row;
		this.end_row = end_row;
	}
	@Override
	public int getWidth() {return end_column - start_column;}
	@Override
	public int getHeight() {return end_row - start_row;}
	@Override
	public int getMinRow() {return start_row;}
	@Override
	public int getMinCol() {return start_column;}
	@Override
	public boolean isValidSpace(Space space)
			{return space.getColumn() >= start_column && space.getColumn() <= end_column && space.getRow() >= start_row && space.getRow() <= end_row;}
	@Override
	public Iterable<Space> allValidSpaces() {
		ArrayList<Space> spaces = new ArrayList<Space>(getWidth() * getHeight());
		for(int x = start_column; x < end_column; ++x)
			for(int y = start_row; y < end_row; ++y)
				spaces.add(Space.get(x, y));
		return spaces;
	}
}
